<?php 
   
   /**
    * The class youtube api to get content from Youtube
    *@since 26/12/2013  
   */
   
   class YoutubeApi{
      /**
       * 
       * The constructor
      */
      public function __construct(){}
      
      /**
       * 
       * Function get info video from Link
       * @access public
       * @param $url string
       * @return array info video
      */
      public static function getInfoMovie($url){
         $id_movie = self::detectedIdMoive($url);
         $link_api = "https://gdata.youtube.com/feeds/api/videos/".$id_movie."?v=2&alt=json";
         $code     = self::get_http_response_code($link_api);
         $array_info = array('code' => $code,'message' => 'Link không tồn tại');
         if($code == 200){
            $respone_youtube = json_decode(file_get_contents($link_api),true);
            $entry = $respone_youtube['entry'];
            $image = $id_movie;
            $title = $entry['title']['$t'];
            $description = $entry['media$group']['media$description']['$t'];
            $array_info = array( 'code' => $code,
                                 'message' => 'Success', 
                                 'data' => array('id' => $id_movie,
                                                 'name' => $title,
                                                 'image' => $image,
                                                 'description' => $description)
                               );   
         }
         return $array_info;
      }
      
      /**
       * 
       * Get list movie from Playlist
       * @param $url string
       * @return array movie
      */
      public static function getMovieFromPlayList($url,$startIndex,$maxResult){
         $id_playlist = self::detectedIdPlaylist($url);
         $link_api = "http://gdata.youtube.com/feeds/api/playlists/".$id_playlist."?max-results=".$maxResult."&start-index=".$startIndex."&alt=json";
         $code     = self::get_http_response_code($link_api);
         $array_return = array('code' => $code,'message' => 'Link không tồn tại');
         if($code == 200){
            $respone_youtube = json_decode(file_get_contents($link_api),true);
            $entry = $respone_youtube['feed']['entry'];
            $array_info = $entry;
            $array_return = array('code' => $code,'message' => 'Success');
            foreach($array_info as $value){
               $id_movie = self::detectedIdMoive($value['link'][2]['href']);
               $array_return['data'][] = array( 'id' => $id_movie,
                                                'title' => $value['title']['$t'],
                                                'duration' => $value['media$group']['media$content'][0]['duration'],
                                                'description' =>$value['content']['$t']
                                        );
            }
              
         }
         return $array_return;
      }
      
      /**
       * 
       * Function detect id from Link
       * @access private
       * @param $link string
       * @return string ID movie
      */
      private static function detectedIdMoive($link){
         $id = '';
         if($link != ''){
            $indexOfW = strpos($link,'watch?v=');
            if($indexOfW){
               $id = substr($link,$indexOfW+8);
            }
            else{
               $indexOfW = strpos($link,'details?v=');
               if($indexOfW)
                  $id = substr($link,$indexOfW+10);
            }
         }
         return $id;
      }
      
      /**
       * 
       * Function detect id from Link
       * @access private
       * @param $link string
       * @return string ID movie
      */
      private static function detectedIdPlaylist($link){
         $id = '';
         if($link != ''){
            $indexOfW = strpos($link,'list=');
            $id = substr($link,$indexOfW+5);
         }
         return $id;
      }
      
      /**
       * 
       * Detect link Youtube is singer video or playlist
       * @param $url link
       * @return int 0 unknow 1 is singer video 2 is playlist 
      */
      public static function detectUrl($url){
         $check = 0;
         if($url != ''){
            if(strpos($url,'playlist?list='))
               $check = 2;
            else{
               if(strpos($url,'watch?v'))
                  $check = 1;
            }
         }
         return $check;
      }
      
      
      /**
       * 
       * Get respone code header url
      */
      public static function get_http_response_code($url) {
        $headers = get_headers($url);
        return substr($headers[0], 9, 3);
      }
   }
?>